//
//  HHTopDelMoreViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/10.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHTopDelMoreViewController.h"

@interface HHTopDelMoreViewController ()<UITableViewDelegate,UITableViewDataSource>{
///因为tableView不会显示999行,显示这么多,系统卡死,如果不放心,可以定义再大些
    BOOL isSelected[999] ;

}

@property(nonatomic, weak) UITableView *tableView;

//玩玩NSMutableArray
@property(nonatomic, strong) NSMutableArray *ArrData;

@end
static NSString *const cellID = @"cellID";
@implementation HHTopDelMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupTableView];
}

-(void)setupTableView{

    UITableView *tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.ArrData = @[].mutableCopy;
    
    for (int i = 0; i<50; i++) {
        isSelected[i] = NO;
        [self.ArrData addObject:[NSString stringWithFormat:@"花花真棒%ld",(long)i]];
    }
    
    _tableView =tableView;
    
}

- (void)buttonClickAction:(UIButton *)button{
    UITableViewCell *cell = (UITableViewCell *)(button.superview.superview);///在iOS6之上是3个.superview
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    isSelected[indexPath.row] = !isSelected[indexPath.row];
    NSString *text = [NSString stringWithFormat:@"点击了: %@",self.ArrData[indexPath.row]];
    [self showHudAutoHideString:text];
    [self.tableView reloadData];
}


#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.ArrData.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)-80, 2, 60, 40)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        button.tag = 333;
        button.backgroundColor = [UIColor orangeColor];
        button.selected =NO;
        [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];

    }
    UIButton *button = (UIButton *)[cell.contentView viewWithTag:333];
    if (isSelected[indexPath.row]) {
        [button setTitle:@"再点我" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor redColor];
    }else{
        [button setTitle:@"点我" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor orangeColor];
    }
    cell.textLabel.text =self.ArrData[indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewRowAction *deleAction =[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        
        [self.ArrData removeObjectAtIndex:indexPath.row];
        
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        [self.ArrData exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        
        NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
        
        
    }];
    UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
    }];
    moreAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    topAction.backgroundColor =[UIColor grayColor];
    return @[topAction,deleAction,moreAction];
}


@end
